<?php
namespace app\admin\controller;
use \think\Controller;
use app\admin\model\Job as JobModel;
use app\admin\model\Depart as DepartModel;

class Job extends Controller
{
    protected $JobModel;
    protected $DepartModel;

    public function __construct()
    {
        if(!session('adminId')){
            $this->redirect('login/index');
        }
        $this->JobModel = new JobModel();
        $this->DepartModel = new DepartModel();
        $this->gid = session('adminGid');
    }

    public function index()
    {
        $query = [
            'ispage' => true,
            'limit' => 12,
            'where' => [
                'gid' => $this->gid
            ],
            'order' => [
                'type' => 'desc',
                'update_time' => 'desc'
            ]
        ];
        $jobRes = $this->JobModel->getLst($query);
        return view('index',[
            'job'=> $jobRes
        ]);
    }

    public function ajaxlst($did)
    {
        $query = [
            'where' => [
                'did' => $did,
                'gid' => $this->gid
            ],
            'order' => [
                'type' => 'desc',
                'update_time' => 'desc'
            ]
        ];
        $jobRes = $this->JobModel->getLst($query);
        $data = [
            'data' => $jobRes
        ];
        return json($data);
    }

    public function add()
    {
        if(request()->isPost()){
            $data = input();
            $data['gid'] = $this->gid;
            $res =  $this->JobModel -> save($data);
            if($res){
                $this->success('新增成功','job/index');
            }else{
                $this->error('新增失败,请检查重试'); 
            }
        };

        $query = [
            'where' => [
                'gid' => $this->gid
            ],
            'order' => [
                'update_time' => 'desc'
            ]
        ];
        $departRes = $this->DepartModel->getLst($query);
        return view('add',[
            'depart' => $departRes
        ]);
    }

    public function edit($id)
    {
        $jobRes = $this->JobModel->get($id);
        if(request()->isPost()){
            $data = input();
            $data['gid'] = $this->gid;
            $res =  $jobRes -> save($data);
            if($res){
                $this->success('编辑成功','job/index');
            }else{
                $this->error('编辑失败,请检查重试'); 
            }
        }
        $query = [
            'where' => [
                'gid' => $this->gid
            ],
            'order' => [
                'update_time' => 'desc'
            ]
        ];
        $departRes = $this->DepartModel->getLst($query);
        return view('edit',[
            'job'=> $jobRes,
            'depart' => $departRes
        ]);
    }

    public function del($id)
    {
        if(request()->isPost()){
            $res =  $this->JobModel -> destroy($id);
            if($res){
                return 1;
            }else{
                return 0;
            }
        };
    }

}
